import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.awt.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.util.Date;

/**
 * 聊天室
 */
class ClientLogin extends JFrame implements ActionListener {

    public static void main(String args[]) {
        new ClientLogin();
    }

    JTextField nameTxt;//设置姓名的
    //	JPasswordField pwdFld;//密码
    TextField tx;

    public ClientLogin() {
        this.init();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//点击就退出
        this.setLayout(null); //取消布局
        this.setVisible(true);//设置可见性
    }

    public void init() {
        this.setTitle("登陆界面----by:Jack魏");//标题
        this.setSize(300, 200);//登录框大小
        int x = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();//获取屏幕的长度
        int y = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();//获取屏幕的宽度
        this.setLocation((x - this.getWidth()) / 2, (y - this.getHeight()) / 2);//让界面居中
        this.setResizable(false);//窗口大小不可改变，true可改变

        Label lb = new Label("登陆信息");   //标签
        lb.setBounds(120, 20, 80, 20);//设置这个标签放哪
        this.add(lb);
        Label lb1 = new Label("用户名:");
        lb1.setBounds(50, 60, 60, 20);//设置这个标签放哪
        this.add(lb1);
        tx = new TextField(); //文本框
        tx.setBounds(130, 60, 110, 22);
        this.add(tx);
        Button bt = new Button("登录");
        bt.setBounds(200, 100, 60, 30);
        this.add(bt);
        bt.addActionListener(this);

    }

    String getname() {
        return tx.getText();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Icon icon = new ImageIcon("1.jpg");//用户自定义图标
        Icon icon1 = new ImageIcon("2.jpg");//用户自定义图标
        try {

            Socket socket = new Socket("127.0.0.1", 8081);//套接字连接服务器
            JOptionPane.showMessageDialog(null, "登陆成功！", "连接成功", JOptionPane.PLAIN_MESSAGE, icon1);//消息对话框
            this.dispose();//销毁登录窗
            new ChatClient(socket, getname());

        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null, "登陆失败！", "连接失败", JOptionPane.PLAIN_MESSAGE, icon);//消息对话框
        }

    }
}

class ChatClient extends JFrame {
    Socket socket;
    JTextArea sendArea;//要发送的内容域
    JTextArea myArea;//消息记录
    String name;

    ChatClient() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//点击就退出
        this.setLayout(null); //取消布局
        this.setVisible(true);//设置可见性
        this.init();
    }

    ChatClient(Socket s, String a) {
        socket = s;
        name = a;
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//点击就退出
        this.setLayout(null); //取消布局
        this.setVisible(true);//设置可见性
        this.init();
        ClientThread thread = new ClientThread(socket, myArea);
        thread.start();
    }

    public void init() {
        this.setTitle(name + "的聊天室");
        this.setSize(600, 600);

        int x = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();//获取屏幕的长度
        int y = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();//获取屏幕的宽度
        this.setLocation((x - this.getWidth()) / 2, (y - this.getHeight()) / 2);//让界面居中
        this.setResizable(false);//窗口大小不可改变，true可改变

        myArea = new JTextArea();
        myArea.setLineWrap(true);//自动换行
        myArea.setBounds(10, 10, 580, 300);

        //JScrollPane sendPanel1=new JScrollPane(myArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//滚动条功能
        this.add(myArea);

        sendArea = new JTextArea();
        sendArea.setLineWrap(true);//自动换行

        sendArea.setBounds(10, 400, 580, 100);

        //JScrollPane logPanel2=new JScrollPane(sendArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//滚动条功能
        this.add(sendArea);

        Button bt1 = new Button("发送");
        Button bt2 = new Button("关闭");
        bt1.setBounds(400, 530, 40, 25);
        bt2.setBounds(500, 530, 40, 25);
        this.add(bt1);
        this.add(bt2);

        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str = sendArea.getText();
                SimpleDateFormat formater = new SimpleDateFormat("HH:mm:ss");

                String time = formater.format(new Date());
                String message = name + " " + time + "说：" + str;

                PrintWriter out = null;
                try {
                    out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                    out.println(message);
                    out.flush();

                } catch (IOException e2) {
                    System.out.println(e2);
                }
                sendArea.setText("");
            }
        });
        bt2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

    }

}

class ClientThread extends Thread {

    Socket socket;
    JTextArea carea;

    public ClientThread(Socket s, JTextArea c) {
        socket = s;
        carea = c;
    }

    @Override
    public void run() {

        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //从数据流读取数据，添加到聊天记录中
            String str = null;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
                carea.append(str + "\n");
            }
        } catch (IOException e3) {
             e3.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
        }
    }
}
